package com.polaris.live.dialog

import android.content.Context
import com.blankj.utilcode.constant.TimeConstants.SEC
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogCallHangUpBinding
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils
import kotlin.math.abs

/**
 * CallHangUpDialog
 *
 * @author Created by 半仙 on 2023/10/11/011 14:10
 */
class CallHangUpDialog(
    context: Context,
    private val callStartTime: Long,
    private val isPay: Boolean,
    private val isFree: Boolean,
    private val isGameCall: Boolean = false,
    private val onCloseCallCallBack: () -> Unit,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_call_hang_up
    }

    private lateinit var mBinding: DialogCallHangUpBinding

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogCallHangUpBinding.bind(popupImplView)

        setData(callStartTime, isPay, isFree, isGameCall)

        mBinding.apply {
            cancel.setOnSingleClickListener {
                dismiss()
            }
            closeCall.setOnSingleClickListener {
                onCloseCallCallBack.invoke()
                dismiss()
            }
        }
    }

    fun setData(callStartTime: Long, isPay: Boolean, isFree: Boolean, isGameCall: Boolean = false) {
        if (!this::mBinding.isInitialized) {
            return
        }
        val safeLevel = UserManager.getUser().flag?.safeLevel
        val safeType = UserManager.getUser().flag?.safeType
        var contents = ""
        if (isGameCall) {
            if (isFree) {
                contents = if (isPay) {
                    StringUtils.getString(string.are_you_sure_games_party)
                } else {
                    StringUtils.getString(string.call_end_sure)
                }
            } else {
                contents = if (isPay) {
                    StringUtils.getString(string.are_you_sure_games_party)
                } else {
                    if (!CommonUtils.isMan(UserManager.getUser().sex)) {
                        woManMessage(safeLevel, safeType)
                    } else {
                        StringUtils.getString(string.call_end_sure)
                    }
                }
            }
        } else {
            if (isFree) {
                contents = if (isPay) {
                    StringUtils.getString(string.are_you_sure_close)
                } else {
                    StringUtils.getString(string.call_end_sure)
                }
            } else {
                contents = if (isPay) {
                    StringUtils.getString(string.are_you_sure_close)
                } else {
                    if (!CommonUtils.isMan(UserManager.getUser().sex)) {
                        woManMessage(safeLevel, safeType)
                    } else {
                        StringUtils.getString(string.call_end_sure)
                    }
                }
            }
        }


        mBinding.titleMessage.text = contents
    }

    private fun woManMessage(safeLevel: Int?, safeType: Int?): String {
        //如果是女非安全用户
        return if (!CommonUtils.isSafeUser(safeLevel, safeType)) {
            StringUtils.getString(string.call_close_tips)
        } else {
            //如果是女 安全用户
            val time = TimeUtils.getTimeSpan(callStartTime, CacheUtil.getServerTime(), SEC)
            val config = AppConfigManager.getConfig(CommonConst.Configs.callMinDuration)
            if (config != null) {
                if (abs(time) <= config.toInt()) {
                    StringUtils.getString(string.call_close_tips_40, config)
                } else {
                    StringUtils.getString(string.call_close_tips)
                }
            } else {
                StringUtils.getString(string.call_close_tips)
            }
        }
    }

}